package com.slife.snap.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileUtils {

    /**
     * 判断文件夹是否存在, 不存在则创建
     * @param path
     */
    public static void checkDirExists(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
        }
    }



    public static void writeToFile(String filePath, String content) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            writer.write(content);
            System.out.println("字符串已成功写入文件：" + filePath);
        } catch (IOException e) {
            System.out.println("写入文件时出现错误：" + e.getMessage());
        }
    }

    public static String dec2Str(String ascii) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < ascii.length() - 1; i += 2) {
            String h = ascii.substring(i, (i + 2));
            // 这里第二个参数传10表10进制
            int decimal = Integer.parseInt(h, 10);
            sb.append((char) decimal);
        }
        return sb.toString();
    }


}
